package com.arron.algorithm.sort;


/**
 * 选择排序
 *  1，选择一个最小值
 */
public class SelectionSort {


    public static void main(String[] args) {

        int[] array = {2,3,1,0,98};

        long timeMillis = System.currentTimeMillis();

        sort(array);
        System.out.println("============耗时："+(System.currentTimeMillis()-timeMillis));
        print(array);

    }


    public static void sort(int[] array){

        if (array == null || array.length<2){
            return;
        }

        for (int i = 0; i < array.length; i++) {
            int minIndex=i;
            for (int j = i; j < array.length; j++) {
                if (array[minIndex]>array[j]){
                    minIndex = j;
                }
            }
            System.out.println("第 "+i+" 轮最小值 ："+array[minIndex]);
            //最小值和array[i]进行交换
            int temp = array[minIndex];
            array[minIndex] = array[i];
            array[i] = temp;

        }


    }


    public static void print(int[] array){

        for (int i = 0; i < array.length; i++) {
            System.out.println(array[i]);
        }
    }
}
